package com.cn.newetv.utils;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cn.newetv.po.VFilmDO;
import com.cn.newetv.po.VSeriesDO;
import com.cn.newetv.po.VSetDO;
import com.cn.newetv.po.VTagDO;
import com.cn.newetv.po.VTypeDO;

public class NWTransform {
	
	public static List<VTagDO> transformJsonToTags(JSONObject jsonObject) throws JSONException {
		List<VTagDO> tags = new ArrayList<VTagDO>();
		JSONArray jsonArrayTags = jsonObject.getJSONArray("list");
		for(int i=0;i<jsonArrayTags.length();i++){
			List<VTypeDO> types = new ArrayList<VTypeDO>();
            JSONObject jsonTag = (JSONObject)jsonArrayTags.opt(i); 
            VTagDO tagDO = new VTagDO(); 
            tagDO.setName(jsonTag.getString("name").trim());
            JSONArray jsonArrayTypes = jsonTag.getJSONArray("types");
            for(int j=0;j<jsonArrayTypes.length();j++){
                JSONObject jsonType = (JSONObject)jsonArrayTypes.opt(j); 
                VTypeDO typeDO = new VTypeDO(); 
                typeDO.setName(jsonType.getString("name")); 
                typeDO.setId(jsonType.getString("id"));
                types.add(typeDO);
            }
            tagDO.setTypes(types);
            tags.add(tagDO);
        }
		return tags;
	}
	
	
	public static List<VFilmDO> transformJsonToList(JSONObject jsonObject) throws JSONException {
		List<VFilmDO> list = new ArrayList<VFilmDO>();
		JSONArray jsonArrayTags = jsonObject.getJSONArray("list");
		for(int i=0;i<jsonArrayTags.length();i++){
            JSONObject jsonList = (JSONObject)jsonArrayTags.opt(i); 
            VFilmDO filmDO = new VFilmDO(); 
            filmDO.setName(jsonList.getString("name").trim());
            filmDO.setType(jsonList.getString("type").trim());
            filmDO.setId(jsonList.getString("id").trim());
            filmDO.setPic(jsonList.getString("pic").trim());
            list.add(filmDO);
        }
		return list;
	}
	
	public static VSeriesDO transformJsonToVSeriesDO(JSONObject jsonObject) throws JSONException {
		VSeriesDO seriesDO = new VSeriesDO();
		seriesDO.setInfo(jsonObject.getString("info"));
		JSONArray jsonArraySets = jsonObject.getJSONArray("list");
		List<VSetDO> setDOs = new ArrayList<VSetDO>();
		for(int i=0;i<jsonArraySets.length();i++){
            JSONObject jsonSet = (JSONObject)jsonArraySets.opt(i); 
            VSetDO set = new VSetDO(); 
            set.setName(jsonSet.getString("name").trim());
            set.setVideoType(jsonSet.getString("video_type").trim());
            set.setSource("http://"+jsonSet.getString("source").trim());
            set.setPic("http://"+jsonSet.getString("pic").trim());
            setDOs.add(set);
        }
		seriesDO.setSets(setDOs);
		return seriesDO;
	}
}
